import java.rmi.RemoteException;

import javax.swing.JOptionPane;

import FlexTrade.WSAdaptor;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DutchStatesDlg.java
 *
 * Created on May 17, 2012, 6:37:10 PM
 */

/**
 *
 * @author hieuht
 * User interface which holds information for existing Dutch auctions
 */
public class DutchStatesDlg extends javax.swing.JDialog {

    /** Creates new form DutchStatesDlg */
    public DutchStatesDlg(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.parent = (ProviderUI) parent;
        initComponents();
        //provider = new Provider();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {
    	this.setTitle("Dutch Auction Status");
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        lblAuctionID = new javax.swing.JLabel();
        txtAuctionID = new javax.swing.JTextField();
        lblProduct = new javax.swing.JLabel();
        txtProduct = new javax.swing.JTextField();
        lblOldAcceptablePrice = new javax.swing.JLabel();
        txtOldAcceptablePrice = new javax.swing.JTextField();
        lblNewAcceptablePrice = new javax.swing.JLabel();
        txtNewAcceptablePrice = new javax.swing.JTextField();
        lblTimeLeft = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        lblAuctionID.setText("Auction ID");

        lblProduct.setText("Product");

        lblOldAcceptablePrice.setText("Old Acceptable Price");

        lblNewAcceptablePrice.setText("New Acceptable Price");

        lblTimeLeft.setText("Time Left:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAuctionID)
                    .addComponent(lblProduct)
                    .addComponent(lblOldAcceptablePrice)
                    .addComponent(lblNewAcceptablePrice))
                .addGap(53, 53, 53)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtNewAcceptablePrice)
                    .addComponent(txtOldAcceptablePrice)
                    .addComponent(txtProduct)
                    .addComponent(txtAuctionID, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(95, 95, 95)
                .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 111, Short.MAX_VALUE)
                .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(103, 103, 103))
            .addGroup(layout.createSequentialGroup()
                .addGap(192, 192, 192)
                .addComponent(lblTimeLeft)
                .addContainerGap(206, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblAuctionID)
                    .addComponent(txtAuctionID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblProduct)
                    .addComponent(txtProduct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblOldAcceptablePrice)
                    .addComponent(txtOldAcceptablePrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNewAcceptablePrice)
                    .addComponent(txtNewAcceptablePrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE)
                .addComponent(lblTimeLeft)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancel)
                    .addComponent(btnOK))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	float newAcceptablePrice = 0;
    	try{
    		newAcceptablePrice = Float.parseFloat(txtNewAcceptablePrice.getText());
    	} catch(Exception e){
    		e.printStackTrace();
    		JOptionPane.showMessageDialog(null, "New acceptable price must be numeric.");
    		return;
    	}
    	try {
			service.receiveNewAcceptablePriceFromProvider(uniqueAuctionID, newAcceptablePrice);
			//update new offer in table of main UI
			this.parent.tblActiveAuction.setValueAt(txtNewAcceptablePrice.getText(),
											this.parent.tblActiveAuction.getSelectedRow(),
													3);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	this.dispose();
    }

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	this.dispose();
    }
    
    public void setUniqueAuctionID(String _uniqueAuctionID){
    	this.uniqueAuctionID = _uniqueAuctionID;
    	txtAuctionID.setText(this.uniqueAuctionID);
    	String strAcceptablePrice = "";
		try {
			strAcceptablePrice = Float.toString(service.getAuctionState(_uniqueAuctionID).getAcceptableBidPrice());
	    	txtOldAcceptablePrice.setText(strAcceptablePrice);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			txtProduct.setText(service.getAuctionPrimitiveData(_uniqueAuctionID).getProduct().getProductName());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DutchStatesDlg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DutchStatesDlg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DutchStatesDlg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DutchStatesDlg.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                DutchStatesDlg dialog = new DutchStatesDlg(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    public void setService(WSAdaptor _adapter) {
    	this.service = _adapter;
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.JLabel lblAuctionID;
    private javax.swing.JLabel lblNewAcceptablePrice;
    private javax.swing.JLabel lblOldAcceptablePrice;
    private javax.swing.JLabel lblProduct;
    private javax.swing.JLabel lblTimeLeft;
    private javax.swing.JTextField txtAuctionID;
    private javax.swing.JTextField txtNewAcceptablePrice;
    private javax.swing.JTextField txtOldAcceptablePrice;
    private javax.swing.JTextField txtProduct;
    // End of variables declaration
    
    String uniqueAuctionID;
    WSAdaptor service;
    ProviderUI parent;
}
